package com.ray.exercise.servlet;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author reny
 * created at 2024/6/21 14:31
 **/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //get请求一般为认证失败跳转请求
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            req.getRequestDispatcher("/index.jsp").forward(req, res);
        } else {
            req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req, res);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //post请求FormAuthenticationFilter会拦截并自动登录，无需跳转登录页
        req.getRequestDispatcher("/index.jsp").forward(req, res);
    }
}
